package com.example.permanentroom;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import java.util.ArrayList;

import Entity.User;
import Entity.commodity;
import Service.ShouyeService;
import Service.UserService;

public class shouye extends AppCompatActivity {
    private EditText password_view;
    private EditText username_view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shouye);

//        TextView tv1=findViewById(R.id.textView1);
//        TextView tv2=findViewById(R.id.textView2);
//        TextView tv3=findViewById(R.id.textView3);
//        TextView tv4=findViewById(R.id.textView4);
//        TextView tv5=findViewById(R.id.textView5);
//        TextView tv6=findViewById(R.id.textView6);
//
//        ImageView iv1=findViewById(R.id.imageView1);
//        ImageView iv2=findViewById(R.id.imageView2);
//        ImageView iv3=findViewById(R.id.imageView3);
//        ImageView iv4=findViewById(R.id.imageView4);
//        ImageView iv5=findViewById(R.id.imageView5);
//        ImageView iv6=findViewById(R.id.imageView6);
//
//        tv1.setText(arr.get(0).cheap());
//        tv2.setText(arr.get(1).cheap());
//        tv3.setText(arr.get(2).cheap());
//        tv4.setText(arr.get(3).cheap());
//        tv5.setText(arr.get(4).cheap());
//        tv6.setText(arr.get(5).cheap());
//
//        Context ctx=getBaseContext();
//        int resId1 = getResources().getIdentifier(arr.get(0).getLogo(), "drawable" , ctx.getPackageName());
//        int resId2 = getResources().getIdentifier(arr.get(1).getLogo(), "drawable" , ctx.getPackageName());
//        int resId3 = getResources().getIdentifier(arr.get(2).getLogo(), "drawable" , ctx.getPackageName());
//        int resId4 = getResources().getIdentifier(arr.get(3).getLogo(), "drawable" , ctx.getPackageName());
//        int resId5 = getResources().getIdentifier(arr.get(4).getLogo(), "drawable" , ctx.getPackageName());
//        int resId6 = getResources().getIdentifier(arr.get(5).getLogo(), "drawable" , ctx.getPackageName());
//
//        iv1.setImageResource(resId1);
//        iv2.setImageResource(resId2);
//        iv3.setImageResource(resId3);
//        iv4.setImageResource(resId4);
//        iv5.setImageResource(resId5);
//        iv6.setImageResource(resId6);
        Gson gson=new Gson();
        Context ctx=getBaseContext();
        ImageView type1=findViewById(R.id.type1);
        ImageView type2=findViewById(R.id.type2);
        ImageView type3=findViewById(R.id.type3);
        int resId1 = getResources().getIdentifier("qiuxie", "drawable" , ctx.getPackageName());
        int resId2 = getResources().getIdentifier("huazhuangpin", "drawable" , ctx.getPackageName());
        int resId3 = getResources().getIdentifier("shoubiao", "drawable" , ctx.getPackageName());
        type1.setImageResource(resId1);
        type2.setImageResource(resId2);
        type3.setImageResource(resId3);

        ArrayList<commodity> arr=new ShouyeService().get_reco();
        TextView tv1=findViewById(R.id.reco1name);
        TextView tv2=findViewById(R.id.reco2name);
        TextView tv3=findViewById(R.id.reco1price);
        TextView tv4=findViewById(R.id.reco2price);
        String Json1 = gson.toJson(arr.get(0));
        String Json2 = gson.toJson(arr.get(1));
        commodity com1 = gson.fromJson(Json1, commodity.class);
        commodity com2 = gson.fromJson(Json2, commodity.class);
        tv1.setText(com1.getCommoname());
        tv2.setText(com2.getCommoname());
        tv3.setText("￥ "+com1.getPrice());
        tv4.setText("￥ "+com2.getPrice());
        ImageView img1=findViewById(R.id.reco1img);
        ImageView img2=findViewById(R.id.reco2img);
        String img1name="item"+com1.getId();
        String img2name="item"+com2.getId();
        int imgresId1 = getResources().getIdentifier(img1name, "drawable" , ctx.getPackageName());
        int imgresId2 = getResources().getIdentifier(img2name, "drawable" , ctx.getPackageName());
        img1.setImageResource(imgresId1);
        img2.setImageResource(imgresId2);
    }



    public void register(View view) {
//        String password = (password_view=(EditText) findViewById(R.id.password)).getText().toString();
//        String username = (username_view=(EditText) findViewById(R.id.username)).getText().toString();
//        User theuser=new User(0,username,password);
//        UserService userService=new UserService(shouye.this);
//        boolean ok=userService.register(theuser);
//       System.out.println(ok);
//        Toast.makeText(this, "注册成功", Toast.LENGTH_LONG).show();
    }
    public void goShouye(View view) {
        Intent intent=new Intent(this,shouye.class);
        startActivity(intent);
        this.finish();
    }
    public void goGouwuche(View view) {
        Intent intent=new Intent(this,gouwuche.class);
        startActivity(intent);
        this.finish();
    }
    public void goWode(View view) {
        Intent intent=new Intent(this,wode.class);
        startActivity(intent);
        this.finish();
    }
    public void goLiebiao_QX(View view) {
        liebiao.type="QX";
        Intent intent=new Intent(this,liebiao.class);
        startActivity(intent);
        this.finish();
    }
    public void goLiebiao_HZP(View view) {
        liebiao.type="HZP";
        Intent intent=new Intent(this,liebiao.class);
        startActivity(intent);
        this.finish();
    }
    public void goLiebiao_SB(View view) {
        liebiao.type="SB";
        Intent intent=new Intent(this,liebiao.class);
        startActivity(intent);
        this.finish();
    }
    public void goTuijian(View view) {
        Intent intent=new Intent(this,tuijian.class);
        startActivity(intent);
        this.finish();
    }
}